package com.tsing.cli.common.doc;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.configuration.SpringDocConfiguration;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springdoc.core.utils.Constants;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

/**
 * SpringDoc 自动配置类
 *
 * @author TheTsing
 */
@AutoConfigureBefore(SpringDocConfiguration.class)
@ConditionalOnProperty(name = Constants.SPRINGDOC_ENABLED, havingValue = "true", matchIfMissing = true)
public class DocAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(OpenAPI.class)
    public OpenAPI openAPI() {
        return new OpenAPI()
                .info(new Info().title("API Documentation").version("snapshot"))
                .schemaRequirement(HttpHeaders.AUTHORIZATION,
                        new SecurityScheme()
                                // 普通 Token
                                //.name(HttpHeaders.AUTHORIZATION)
                                //.type(SecurityScheme.Type.APIKEY)
                                //.in(SecurityScheme.In.HEADER)
                                // Bearer Token
                                .name(HttpHeaders.AUTHORIZATION)
                                .type(SecurityScheme.Type.HTTP)
                                .scheme("bearer"))
                .addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION));
    }

    @Bean
    public GlobalOpenApiCustomizer globalOpenApiCustomizer() {
        return openApi -> openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream()).forEach(operation -> {
            operation.getResponses().addApiResponse(String.valueOf(HttpStatus.BAD_REQUEST.value()),
                    new ApiResponse().description("客户端请求存在语法错误或业务异常").content(new Content().addMediaType(MediaType.TEXT_PLAIN_VALUE, new io.swagger.v3.oas.models.media.MediaType().example(HttpStatus.BAD_REQUEST.getReasonPhrase()))));
            operation.getResponses().addApiResponse(String.valueOf(HttpStatus.UNAUTHORIZED.value()),
                    new ApiResponse().description("认证失败").content(new Content().addMediaType(MediaType.TEXT_PLAIN_VALUE, new io.swagger.v3.oas.models.media.MediaType().example(HttpStatus.UNAUTHORIZED.getReasonPhrase()))));
            operation.getResponses().addApiResponse(String.valueOf(HttpStatus.FORBIDDEN.value()),
                    new ApiResponse().description("没有权限").content(new Content().addMediaType(MediaType.TEXT_PLAIN_VALUE, new io.swagger.v3.oas.models.media.MediaType().example(HttpStatus.FORBIDDEN.getReasonPhrase()))));
            operation.getResponses().addApiResponse(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),
                    new ApiResponse().description("服务器内部错误").content(new Content().addMediaType(MediaType.TEXT_PLAIN_VALUE, new io.swagger.v3.oas.models.media.MediaType().example(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()))));
        });
    }

    @Bean
    @Primary
    public SwaggerUiConfigProperties swaggerUiConfig() {
        SwaggerUiConfigProperties config = new SwaggerUiConfigProperties();
        config.setFilter("true");
        config.setDocExpansion("none");
        config.setShowCommonExtensions(true);
        config.setPersistAuthorization(true);
        config.setDefaultModelExpandDepth(10);
        config.setDefaultModelsExpandDepth(10);
        config.setDisplayRequestDuration(true);
        config.setDisableSwaggerDefaultUrl(true);
        return config;
    }

}
